package org.cheniy.domain.strategy.repository;

import org.cheniy.domain.strategy.model.entity.StrategyAwardEntity;
import org.cheniy.domain.strategy.model.entity.StrategyEntity;
import org.cheniy.domain.strategy.model.entity.StrategyRuleEntity;
import org.cheniy.domain.strategy.model.valobj.RuleTreeVO;
import org.cheniy.domain.strategy.model.valobj.RuleWeightVO;
import org.cheniy.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import org.cheniy.domain.strategy.model.valobj.StrategyAwardStockKeyVO;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cheniy
 * @description Strategy需要的调用的持久化接口
 * @create 2025/3/1 23:40
 */

public interface IStrategyRepository {

    /**
     * 查询策略活动下的奖品列表
     * @param strategyId
     * @return
     */
    List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId);

    /**
     * 将配置好的配置活动策略保存到redis中
     * @param strategyId
     * @param rateRange 奖品的概率范围
     * @param shuffleStrategyAwardSearchRateTables
     */
    void storeStrategyAwardSearchRateTable(String strategyId, BigDecimal rateRange, HashMap<String, String> shuffleStrategyAwardSearchRateTables);

    /**
     * 根据策略id获取到概率范围
     * @param strategyId
     * @return
     */
    Integer getRateRange(Long strategyId);

    Integer getRateRange(String key);


    /**
     * 根据策略id和随机数获取到奖品id
     * @param strategyId
     * @param rateRandom
     * @return
     */
    Integer getAwardMapAwardIdByRandom(String strategyId, int rateRandom);

    /**
     * 根据策略id查询Strategy实体
     * @param strategyId
     * @return
     */
    StrategyEntity queryStrategyByStrategyId(Long strategyId);

    /**
     * 查询StrategyRule实体
     * @param strategyId 策略id
     * @param ruleWeight ruleWeight名称
     * @return
     */
    StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleWeight);

    /**
     * 查询具体的规则信息
     * @param strategyId
     * @param awardId
     * @param ruleModel
     * @return
     */
    String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel);

    /**
     * 查询奖品规则
     * @param strategyId
     * @param awardId
     * @return
     */
    StrategyAwardRuleModelVO queryStrategyAwardRule(Long strategyId, Integer awardId);

    /**
     * 通过TreeId查询该规则树下的节点并装配到RuleTreeVO中
     * @param treeId
     * @return
     */
    RuleTreeVO queryRuleTreeVOByTreeId(String treeId);


    /**
     * 缓存奖品库存
     * @param cacheKey
     * @param awardCount
     */
    void cacheStrategyAwardCount(String cacheKey, Integer awardCount);


    /**
     * 缓存key  desr方式扣减库存
     * @param cacheKey
     * @return
     */
    Boolean subtractionAwardStock(String cacheKey);

    /**
     * 将奖品加入到延迟队列中去
     * @param strategyAwardStockKeyVO
     */
    void awardStockConsumeSendQueue(StrategyAwardStockKeyVO strategyAwardStockKeyVO);

    /**
     * 获取到延迟队列中的值
     * @return
     */
    StrategyAwardStockKeyVO takeQueueValue();

    /**
     * 根据延迟队列中的值更新数据库
     * @param strategyId
     * @param awardId
     */
    void updateStrategyAwardStock(Long strategyId, Integer awardId);

    /**
     * 查询规则奖品实体
     * @param strategyId
     * @param awardId
     * @return
     */
    StrategyAwardEntity queryStrategyAwardEntity(Long strategyId, Integer awardId);

    /**
     * 根据活动id查询策略id
     * @param activityId
     * @return
     */
    Long queryStrategyIdByActivityId(Long activityId);

    /**
     * 查询用户今日抽奖次数
     * @param userId
     * @param strategyId
     * @return
     */
    Integer queryTodayUserRaffleCount(String userId, Long strategyId);

    /**
     * 根据规则树ID集合查询奖品中加锁数量的配置「部分奖品需要抽奖N次解锁」
     *
     * @param treeIds 规则树ID值
     * @return key 规则树，value rule_lock 加锁值
     */

    Map<String, Integer> queryAwardRuleLockCount(String[] treeIds);

    /**
     * 缓存key，decr 方式扣减库存
     *
     * @param cacheKey 缓存Key
     * @param endDateTime 活动结束时间
     * @return 扣减结果
     */
    Boolean subtractionAwardStock(String cacheKey, Date endDateTime);


    /**
     * 根据用户ID、策略ID，查询用户活动账户总使用量
     *
     * @param userId     用户ID
     * @param strategyId 策略ID
     * @return 使用总量
     */
    Integer queryActivityAccountTotalUseCount(String userId, Long strategyId);

    /**
     * 查询奖品权重配置
     *
     * @param strategyId 策略ID
     * @return 权重规则
     */
    List<RuleWeightVO> queryAwardRuleWeight(Long strategyId);

}
